package neves.android.etmg.pathfinding;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import android.util.Log;

import neves.android.etmg.sprite.MovableObject;

import neves.android.etmg.tilemap.TileMap;

public class AStarSearchWithBSP_TileMap extends AStarSearch implements
		PathFinder {

	//private BSPTree bspTree;

	private TileMap curTMap;
	private TileMapNodeKeeper mapNodes;

	public AStarSearchWithBSP_TileMap(TileMap newMap) {
		setTileMap(newMap);

	}

	public void setTileMap(TileMap newMap) {
		curTMap = newMap;
		mapNodes = new TileMapNodeKeeper(curTMap);
	}

	@Override
	public List find(MovableObject a, MovableObject b) {
		// TODO Auto-generated method stub 

		TileMapPathNode startNode = mapNodes.putNewNode(a.GetPos());
		TileMapPathNode goalNode = mapNodes.putNewNode(b.GetPos());
		goalNode.setIsGoal(true);

		// if start and goal is in the same leaf, no need to do
		// A* search
		if (startNode == goalNode) {
			return null;
		}

		if (startNode != null && goalNode != null) {

			//Log.w("ETMG Path", "Start PathFinding");
			List path = super.findPath(startNode, goalNode);

			priintPath(convertPath(path));
			return convertPath(path);
		}

		return null;
	}

	/**
	Converts path of AStarNodes to a path of Vector3D
	locations.
	 */
	protected List convertPath(List path) {
		LinkedList pathList = new LinkedList();
		for(int temi =0 ; temi<path.size();temi++){
			
			pathList.addLast(((TileMapPathNode)path.get(temi)).location);
			
		}
		return pathList;
	}

	
	private void priintPath(List path){
		
		String pathString = "";
		
		for(int temi =0 ; temi<path.size();temi++){
			int[] temLoc = ((int[])path.get(temi));
			pathString += Integer.toString(temLoc[0])+","+Integer.toString(temLoc[1])+" ";
			
		}
		//Log.w("ETMG",pathString);
	}
	
}
